package com.xinqi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.dto.Result;
import com.xinqi.entity.Blog;
import com.xinqi.entity.User;
import com.xinqi.service.BlogService;
import com.xinqi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xin麒
 * @date 2023/6/25 15:21
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api("用户模块")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("{page}/{limit}")
    @ApiOperation("查看某页用户")
    public Result findPageQueryBook(@PathVariable Long page, @PathVariable Long limit  ) {
        //创建page对象
        Page<User> pageParam = new Page<>(page,limit);
        //调用service方法
        List<User> users = userService.getPageByLimitationsAndPages(pageParam);
        //返回
        return Result.ok(users);
    }


    @PostMapping
    @ApiOperation("新增用户")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return Result.ok("新增user成功!");
    }

    @GetMapping("/{id}")
    @ApiOperation("user信息")
    public Result get(@PathVariable("id") Long id){
        return Result.ok(userService.getById(id));
    }
}
